// CherryMerry ( Mikhail Tomilenko ), Reverse Entertainment, 2021-2024


#include "../Public/PlayerLevelManager.h"
#include "../Public/PlayerHealthManager.h"

#include <Kismet/KismetMathLibrary.h>


UPlayerLevelManager::UPlayerLevelManager()
{

	PrimaryComponentTick.bCanEverTick = false;

	MaxLVL = 10;
	MaxEXP = 13200;
}


float UPlayerLevelManager::GetHealthFromLVL(int32 LVL)
{
	float NewHealth = 0;

	switch (LVL)
	{
	case 1:
		NewHealth = 75;
		break;
	case 2:
		NewHealth = 75;
		break;
	case 3:
		NewHealth = 75;
		break;
	case 4:
		NewHealth = 75;
		break;
	case 5:
		NewHealth = 75;
		break;
	case 6:
		NewHealth = 75;
		break;
	case 7:
		NewHealth = 75;
		break;
	case 8:
		NewHealth = 75;
		break;
	case 9:
		NewHealth = 75;
		break;
	case 10:
		NewHealth = 75;
		break;
	}

	return NewHealth;
}

float UPlayerLevelManager::GetShieldFromLVL(int32 LVL)
{
	float NewShield = 0;

	switch (LVL)
	{
	case 1:
		NewShield = 75;
		break;
	case 2:
		NewShield = 75;
		break;
	case 3:
		NewShield = 75;
		break;
	case 4:
		NewShield = 75;
		break;
	case 5:
		NewShield = 75;
		break;
	case 6:
		NewShield = 75;
		break;
	case 7:
		NewShield = 75;
		break;
	case 8:
		NewShield = 75;
		break;
	case 9:
		NewShield = 75;
		break;
	case 10:
		NewShield = 75;
		break;
	}

	return NewShield;
}

void UPlayerLevelManager::BeginPlay()
{
	Super::BeginPlay();

}


void UPlayerLevelManager::UpdateLVL()
{
	int32 CurrentMaxEXPOnLVL = MaxEXPFromLVL(CurrentLVL);
	int32 NextMaxEXPOnLVL = MaxEXPFromLVL(CurrentLVL + 1);

	if (CurrentEXP > CurrentMaxEXPOnLVL)
	{
		if (CurrentEXP > NextMaxEXPOnLVL && CurrentEXP < MaxEXPFromLVL(CurrentLVL))
		{
			OnLVLUp(CalculateLVLFromEXP(CurrentEXP), CalculateLVLPercentFromEXP(CurrentEXP));
		}
	}
}

void UPlayerLevelManager::TakeEXP(int32 EXPToAdd)
{
	CurrentEXP = CurrentEXP + EXPToAdd;

	UPlayerUIManager* UIManager = GetOwner()->FindComponentByClass<UPlayerUIManager>();
	UIManager->PlayerHUDMenu->PlayerLVLInfoWidget->UpdateWidgetInfo(CurrentLVL, CurrentEXP, MaxEXPOnLVL, PercentOnLVL);

	UpdateLVL();
}

void UPlayerLevelManager::OnLVLUp(int32 NewLVL, float NewPercentOnLVL)
{
	if (CurrentEXP > MaxEXPFromLVL(CurrentLVL))
	{
		CurrentEXP -= MaxEXPFromLVL(CurrentLVL);
		UE_LOG(LogTemp, Warning, TEXT("CurrentEXP %f"));
	}
	else if (CurrentEXP == MaxEXPFromLVL(CurrentLVL))
	{
		CurrentEXP = 0;
		UE_LOG(LogTemp, Warning, TEXT("CurrentEXP %f"));
	}
	CurrentLVL = NewLVL;
	PercentOnLVL = NewPercentOnLVL;

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("LVL UP!\nNew LVL: %f"), CurrentLVL));

	UPlayerHealthManager* HealthManager = GetOwner()->FindComponentByClass<UPlayerHealthManager>();
	if (HealthManager->CurrentTotemsAmount != HealthManager->MaxTotemsAmount)
	{
		HealthManager->AddTotem();
	}
}

int32 UPlayerLevelManager::MaxEXPFromLVL(int32 LVL)
{

	if (LVL == 1)
	{
		MaxEXPOnLVL = 500;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 2)
	{
		MaxEXPOnLVL = 800;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 3)
	{
		MaxEXPOnLVL = 1200;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 4)
	{
		MaxEXPOnLVL = 1900;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 5)
	{
		MaxEXPOnLVL = 2700;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 6)
	{
		MaxEXPOnLVL = 3900;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 7)
	{
		MaxEXPOnLVL = 5400;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 8)
	{
		MaxEXPOnLVL = 7000;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 9)
	{
		MaxEXPOnLVL = 9400;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}
	else if (LVL == 10)
	{
		MaxEXPOnLVL = MaxEXP;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Case %f"), CurrentLVL));
	}

	return MaxEXPOnLVL;
}

int32 UPlayerLevelManager::CalculateLVLFromEXP(int32 EXPToCalculateLVL)
{
	int32 FoundedLVL = 0;

	for (int32 LVLForSearch = CurrentLVL; LVLForSearch != MaxLVL; LVLForSearch++)
	{
		int32 MaxFoundedEXPOnLVL = MaxEXPFromLVL(LVLForSearch);

		if (EXPToCalculateLVL > MaxFoundedEXPOnLVL)
		{
			FoundedLVL = LVLForSearch;
			break;
		}
	}
	return FoundedLVL;
}

float UPlayerLevelManager::CalculateLVLPercentFromEXP(int32 EXPToCalculatePercent)
{
	float EXPToCalculate = EXPToCalculatePercent;
	float MaxEXPToNormalize = MaxEXPFromLVL(CurrentLVL);

	PercentOnLVL = UKismetMathLibrary::NormalizeToRange(EXPToCalculate, 0, MaxEXPToNormalize);

	return PercentOnLVL;
}
